img = Image.open('./house.jpg')
# 将读入的图片转化为float32类型的numpy.ndarray
# 图片读入成ndarry时,形状是[H,W,3]
x = np.array(img).astype('float32')#(282,378,3) 
# 将图片形状调整为[N,C,H,W]的格式,以满足F.conv2d数据格式
x = np.transpose(x,(2,0,1))
x = x[np.newaxis,:]
x = torch.Tensor(x) #(1,3,282,378)

# 设置初始值
w = np.array([[-1,-1,-1],
              [-1,8,-1],
              [-1,-1,-1]], dtype='float32')

# w 的axis=1维度上数组重复3次,让输入的通道数为3
# 形状为：(1,3,3,3)[out_channels, in_channels/groups,Kh, Kw] 
w = w.reshape(1,1,3,3) 

# 向量化 (out_channels, in_channels/groups,Kh, Kw)
w = np.repeat(w, 3, axis=1)
w = torch.Tensor(w)  

# 边缘检测,对输入的图像进行卷积操作
y = F.conv2d(input = x, 
             weight = w,
             stride = 1) 
#查看 y形状,输出通道数为 1,out_channels=1一致
print(y.shape)#(1, 1,280, 376) 
